package com.mybatisplus.mp.gc;

public class GcDemo {
    private static GcDemo gcDemo = null;

    private int[] a = new int[20000];

    public void isAlive() {
        System.out.println("still alive");
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        System.out.println("finalize执行");
        gcDemo = this;
    }
//-XX:+PrintGCDetails
    public static void main(String[] args) throws InterruptedException {
        gcDemo = new GcDemo();
        //第一次拯救自己
        gcDemo = null;
        System.gc();
        //因为finalize()优先级低，所以让线程暂停0.5s
        Thread.sleep(500);
        if (gcDemo != null) {
            gcDemo.isAlive();
        }

        //自救失败
        gcDemo = null;
        System.gc();
        //因为finalize()优先级低，所以让线程暂停0.5s
        Thread.sleep(500);
        if (gcDemo != null) {
            gcDemo.isAlive();
        }else {
            System.out.println("dead");
        }

    }
}
